package com.ibm.workplace.wcm.cqplugin.ui.preference;

import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.preference.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.ibm.workplace.wcm.cqplugin.*;
import com.ibm.workplace.wcm.cqplugin.cq.CqConnectionProfile;
import com.ibm.workplace.wcm.cqplugin.preference.*;

public class ConfigureCqConnectionProfilesComposite extends Composite implements SelectionListener {

    private Table table = null;
    private Composite composite = null;
    private Button addButton = null;
    private Button editButton = null;
    private Button removeButton = null;

    public ConfigureCqConnectionProfilesComposite(Composite arg0, int arg1) {
        super(arg0, arg1);

        initialize();
    }

    private void initialize() {
        GridLayout gridLayout = new GridLayout(2, false);
                
        Label label = new Label(this, 0);
        label.setText("Configure CQ connection profiles:");
        GridData data = new GridData();
        data.horizontalSpan = 2;
        label.setLayoutData(data);
        
        table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        data = new GridData();
        data.heightHint = 200;
        data.widthHint = 80 + 80 + 140 + 60 + 80 + 10;
        table.setLayoutData(data);
        table.addSelectionListener(this);

        TableColumn tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setWidth(80);
        tableColumn.setText("Name");
/*
        tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setWidth(80);
        tableColumn.setText("Family");
*/
        TableColumn tableColumn1 = new TableColumn(table, SWT.NONE);
        tableColumn1.setWidth(140);
        tableColumn1.setText("Host");

        TableColumn tableColumn2 = new TableColumn(table, SWT.NONE);
        tableColumn2.setWidth(60);
        tableColumn2.setText("Port");

        TableColumn tableColumn3 = new TableColumn(table, SWT.NONE);
        tableColumn3.setWidth(140);
        tableColumn3.setText("Schema");

        TableColumn tableColumn4 = new TableColumn(table, SWT.NONE);
        tableColumn4.setWidth(80);
        tableColumn4.setText("User Name");
        
        this.setLayout(gridLayout);
        createComposite();
    }

    /**
     * This method initializes composite	
     *
     */
    private void createComposite() {
        composite = new Composite(this, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        
        addButton = new Button(composite, SWT.NONE);
        addButton.setText("&Add");
        addButton.addSelectionListener(this);
        addButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

        editButton = new Button(composite, SWT.NONE);
        editButton.setText("&Edit");
        editButton.addSelectionListener(this);
        editButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

        removeButton = new Button(composite, SWT.NONE);
        removeButton.setText("&Remove");
        removeButton.addSelectionListener(this);
        removeButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

        updateEnableStates();
    }

    public void widgetDefaultSelected(SelectionEvent event) {
        if (event.getSource() == table) {
            editPressed();
        }
    }

    public void widgetSelected(SelectionEvent event) {
        if (event.getSource() == addButton) {
            AddCqConnectionProfileDialog dialog = new AddCqConnectionProfileDialog(getShell());
            if (dialog.open() == dialog.OK) {
                addTableItem(dialog.getProfile());
                if (table.getItemCount() > 0) {
                    table.setSelection(table.getItemCount() - 1);
                }
            }
        } else if (event.getSource() == editButton) {
            editPressed();
        } else if (event.getSource() == removeButton) {
            if (table.getSelectionCount() == 1) {
                if (MessageDialog.openQuestion(getShell(), "Remove Connection Profile", "Are you sure you want to remove this connection profile?")) {
                    table.remove(table.getSelectionIndex());
                }
            }
        }

        updateEnableStates();
    }

    protected void editPressed() {
        if (table.getSelectionCount() == 1) {
            CqConnectionProfile ccp = (CqConnectionProfile) table.getSelection()[0].getData();

            AddCqConnectionProfileDialog dialog = new AddCqConnectionProfileDialog(getShell(), ccp);
            if (dialog.open() == dialog.OK) {
                editTableItem(table.getSelection()[0], dialog.getProfile());
            }
        }
    }

    private void updateEnableStates() {
        editButton.setEnabled(table.getItemCount() > 0 && table.getSelectionCount() > 0);
        removeButton.setEnabled(table.getItemCount() > 0 && table.getSelectionCount() > 0);
    }

    public void init(IPreferenceStore store) {
        table.removeAll();

        boolean continueAdding = true;
        int index = 0;
        while (continueAdding) {
            String name = store.getString(CqPreferenceConstants.PREF_PROFILE_NAME + (index == 0 ? "" : index + ""));
            if (name != null && !name.trim().equals("")) {
                CqConnectionProfile profile = new CqConnectionProfile();
                profile.setName(name);
                //profile.setFamily(store.getString(CqPreferenceConstants.PREF_FAMILY + (index == 0 ? "" : index + "")));
                profile.setHost(store.getString(CqPreferenceConstants.PREF_HOST + (index == 0 ? "" : index + "")));
                profile.setPort(store.getString(CqPreferenceConstants.PREF_PORT + (index == 0 ? "" : index + "")));
                profile.setUserName(store.getString(CqPreferenceConstants.PREF_USER_NAME + (index == 0 ? "" : index + "")));
                profile.setPassword(store.getString(CqPreferenceConstants.PREF_PASSWORD + (index == 0 ? "" : index + "")));
                profile.setSchema(store.getString(CqPreferenceConstants.PREF_SCHEMA + (index == 0 ? "" : index + "")));

                addTableItem(profile);

                index++;
            } else {
                continueAdding = false;
            }
        }
    }

    public void store(IPreferenceStore store) {
        for (int i = 0; i < table.getItemCount() + 1; i++) {
            CqConnectionProfile profile;
            if (i < table.getItemCount()) {
                profile = (CqConnectionProfile) table.getItem(i).getData();
            } else {
                profile = new CqConnectionProfile(); // we want to store 1 blank entry at the end so the code that loads will quit load entrties
            }
            store.setValue(CqPreferenceConstants.PREF_PROFILE_NAME + (i == 0 ? "" : i + ""), profile.getName());
            //store.setValue(CqPreferenceConstants.PREF_FAMILY + (i == 0 ? "" : i + ""), profile.getFamily());
            store.setValue(CqPreferenceConstants.PREF_HOST + (i == 0 ? "" : i + ""), profile.getHost());
            store.setValue(CqPreferenceConstants.PREF_PORT + (i == 0 ? "" : i + ""), profile.getPort());
            store.setValue(CqPreferenceConstants.PREF_USER_NAME + (i == 0 ? "" : i + ""), profile.getUserName());
            store.setValue(CqPreferenceConstants.PREF_PASSWORD + (i == 0 ? "" : i + ""), profile.getPassword());
            store.setValue(CqPreferenceConstants.PREF_SCHEMA + (i == 0 ? "" : i + ""), profile.getSchema());
        }
    }

    private void addTableItem(CqConnectionProfile profile) {
        TableItem tableItem = new TableItem(table, 0);
        editTableItem(tableItem, profile);
    }

    private void editTableItem(TableItem tableItem, CqConnectionProfile profile) {
        if (tableItem == null || profile == null) return;

        tableItem.setText(new String[] { profile.getName(), profile.getHost(), profile.getPort(), profile.getSchema(), profile.getUserName() });
        tableItem.setData(profile);
    }

}
